package com.bite.springbook.service;

import com.bite.springbook.dao.BookDao;
import com.bite.springbook.enums.StatusEnum;
import com.bite.springbook.mapper.BookMapper;
import com.bite.springbook.model.BookInfo;
import com.bite.springbook.model.PageResult;
import com.bite.springbook.model.RequestPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookDao bookDao;
    @Autowired
    private BookMapper bookMapper;

    public Integer addBook(BookInfo bookInfo) {
        return bookMapper.intsertBook(bookInfo);
    }

    public PageResult<BookInfo> getLsitByPage(RequestPage requestPage) {
        Integer count = bookMapper.count();
        List<BookInfo> bookinfos = bookMapper.getListByPage(requestPage.getOffset() , requestPage.getPageSize());
        for(BookInfo bookInfo : bookinfos){
            bookInfo.setStateCN(StatusEnum.getNameByCode(bookInfo.getStatus()).getName());
        }
        return new PageResult<BookInfo>(bookinfos, count , requestPage);
    }

    public BookInfo getBookInfo(Integer bookId) {
        return bookMapper.getBookInfo(bookId);
    }

    public Integer updateBookIfo(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids) {
        return bookMapper.batchDelete(ids);
    }
}
